package ru.digitanoise.dnaccountapi.controllers;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ru.digitanoise.dnaccountapi.mapper.AccountMapper;
import ru.digitanoise.dnaccountapi.models.dto.AccountResponseDTO;
import ru.digitanoise.dnaccountapi.models.dto.RegistrationAccountRequestDTO;
import ru.digitanoise.dnaccountapi.services.AccountService;

@RestController
@RequestMapping("/dn/account/api/v1")
@RequiredArgsConstructor
public class AccountController {

    private final AccountService accountService;
    private final AccountMapper accountMapper;

    @PostMapping(value = "/sing-up", consumes = {"multipart/form-data"})
    @ResponseBody
    public ResponseEntity<Void> createAccount(
            @RequestBody RegistrationAccountRequestDTO registrationAccountRequestDTO,
            @RequestPart(value = "avatar", required = false) MultipartFile avatar
    ) {
        accountService.saveAccount(
                accountMapper.map(registrationAccountRequestDTO),
                avatar
        );
        return ResponseEntity.ok().build();
    }

    @GetMapping("/get")
    @ResponseBody
    public ResponseEntity<AccountResponseDTO> getAccount(
            @RequestHeader(name = "ACCOUNT-ID") Long accountId
    ) {
        return ResponseEntity.ok(
                accountMapper.map(
                        accountService.getAccountById(accountId)
                )
        );
    }
}
